﻿//-----------------------------------------------------------------------------------------------------------
//  Description:
//      简历业务逻辑
//      
//      
//  Comment                                Date                 Author      Email
//   ----------------------------    -------             -------      -----------------------------------
//     Create                                   2012/11/12   Hu Yibin    yibin.hu@qq.com
//
// 
//-----------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Core;
using Forest.Core.Data;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Cat.Domain.Services.Resumes;

namespace Forest.Cat.Domain.Services
{
    public partial class ResumeService : IResumeService
    {
        private readonly IRepository<Resume> _resumeRepository;
        private readonly IWorkExperienceService _workService;
        private readonly ITrainingService _trainingService;
        private readonly ISkillService _skillService;
        private readonly IEducationService _educationService;
        private readonly ICertificationService _certificationService;
        private readonly IGenerateNumberService _generateNumberService;


        public ResumeService(IRepository<Resume> resumeRepository, IWorkExperienceService workService
            , ISkillService skillService, ITrainingService trainingService, IEducationService educationService
            , ICertificationService certificationService, IGenerateNumberService generateNumberService)
        {
            _resumeRepository = resumeRepository;
            _workService = workService;
            _skillService = skillService;
            _trainingService = trainingService;
            _educationService = educationService;
            _certificationService = certificationService;
            _generateNumberService = generateNumberService;
        }

        #region Base Methods

        #region Resume
        public virtual void DeleteResume(Resume resume)
        {
            if (resume == null)
                throw new ArgumentNullException("Resume");

            _resumeRepository.Delete(resume);
        }

        public Resume GetResumeById(Guid resumeId)
        {
            if (resumeId == Guid.Empty)
                return null;
            return _resumeRepository.GetById(resumeId);
        }

        public virtual IPagedList<Resume> GetAllResumes(int pageIndex, int pageSize)
        {
            var query = _resumeRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Resume>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertResume(Resume resume)
        {
            if (resume == null)
                throw new ArgumentNullException("Resume");
            _resumeRepository.Insert(resume);
        }

        public virtual void UpdateResume(Resume resume)
        {
            if (resume == null)
                throw new ArgumentNullException("Resume");
            _resumeRepository.Update(resume);
        }

        /// <summary>
        ///  获取求职者的所有简历
        /// </summary>
        /// <param name="jobseekerId"></param>
        /// <returns></returns>
        public virtual IList<Resume> GetResumes(Guid jobseekerId)
        {
            var query = _resumeRepository.Table;

            query = query.Where(r => r.JobseekerId == jobseekerId && r.Deleted == null).OrderBy(r => r.LastUpdated);
            var resumes = query.ToList();

            return resumes;
        }
        #endregion

        #region Education
        #endregion

        #region WorkExperience
        #endregion

        #region Education
        #endregion

        #region Skill
        #endregion

        #region Training
        #endregion

        #region Certification
        #endregion



        #endregion

        public void InsertSkill(Skill skill)
        {

        }

        public void UpdateSkill(Skill skill)
        {
        }

        public virtual IQueryable<Resume> GetAllResume(Guid jobseekerId)
        {
            var query = _resumeRepository.Table;
            query = query.Where(item => item.JobseekerId == jobseekerId).OrderBy(r => r.LastUpdated);
            return query;
        }

        public Guid CreateId
        {
            get
            {
                return _generateNumberService.GenerateId<Resume>(_resumeRepository);
            }
        }

        public void MarkAsDeleted(Guid id)
        {
             var entity = this.GetResumeById(id);
             if (entity != null)
             {
                 entity.Deleted = true;
                 entity.LastUpdated = DateTime.Now;
                 _resumeRepository.Update(entity);
             }
        }
        public void DeleteResumeAndDetial(Guid id)
        {
            var entity = this.GetResumeById(id);
            if (entity != null)
            {
                // 删除work
                var workList = _workService.GetWorkExperiences(id);
                foreach (var item in workList)
                {
                    _workService.DeleteWorkExperience(item);
                }

                // 删除skill
                var skillList = _skillService.GetSkills(id);
                foreach (var item in skillList)
                {
                    _skillService.DeleteSkill(item);
                }

                // 删除training
                var trainingList = _trainingService.GetTrainings(id);
                foreach (var item in trainingList)
                {
                    _trainingService.DeleteTraining(item);
                }

                // 删除education
                var educationList = _educationService.GetEducations(id);
                foreach (var item in educationList)
                {
                    _educationService.DeleteEducation(item);
                }

                // 删除certification
                var certificationList = _certificationService.GetCertifications(id);
                foreach (var item in certificationList)
                {
                    _certificationService.DeleteCertification(item);
                }

                this.DeleteResume(entity);
            }
        }
    }
}
